package com.alien.admin.modules.utils;

import com.alien.admin.utils.StringUtils;
import com.google.common.collect.Lists;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2014-2021, 厦门神州鹰软件科技有限公司
 * FileName: MyDateUtils
 *
 * @author: zengzf
 * Date:     2021/11/12 10:03
 * Description:
 * History:
 */
public class MyDateUtils {

    public static Date getTodayBegin() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    public static Date getTodayEnd() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 23);
        todayStart.set(Calendar.MINUTE, 59);
        todayStart.set(Calendar.SECOND, 59);
        todayStart.set(Calendar.MILLISECOND, 59);
        return todayStart.getTime();
    }

    public static String convertDateToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static List<Date> getTodayDate() {
        return Lists.newArrayList(getTodayBegin(), getTodayEnd());
    }

    public static List<Timestamp> getTodayTimestampList() {
        Date begin = getTodayBegin();
        Date end = getTodayEnd();
        return Lists.newArrayList(new Timestamp(begin.getTime()), new Timestamp(end.getTime()));
    }

    public static List<Timestamp> getOneDayTimestampList(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return getTodayTimestampList();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begin = null;
        Date end = null;
        try {
            begin = simpleDateFormat.parse(dateStr + " 00:00:00");
            end = simpleDateFormat.parse(dateStr + " 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Lists.newArrayList(new Timestamp(begin.getTime()), new Timestamp(end.getTime()));
    }

    /**
     * 获取n天前的时间点
     *
     * @param n
     * @return
     */
    public static Date getNDayAgo(int n) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -n);
        Date nDayAgo = calendar.getTime();
        return nDayAgo;
    }
}

